package chapter12;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/3/12 10:36
 */
public class InputFile {
    private BufferedReader reader;

    public InputFile(String file) throws Exception {
        try {
            reader = new BufferedReader(new FileReader(file));
        } catch (FileNotFoundException e) {
            // 此时文件还未打开，因此不用关闭文件
            System.out.println("could not open file " + file);
            throw e;
        } catch (Exception e) {
            // 此时文件已经打开，必须关闭已经打开的文件
            try {
                reader.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            throw e;
        } finally {

        }
    }

    public String getLine() {
        String s;
        try {
            s = reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException("readline() failed");
        }
        return s;
    }

    public void dispose() {
        try {
            reader.close();
            System.out.println("dispose success");
        } catch (IOException e) {
            throw new RuntimeException("dispose failed");
        }
    }
}
